﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Newtonsoft.Json.Linq;
using Sparklings.JsonUtils;
using Sparklings.RayTracing;
using Sparklings.Rendering;
using System.Collections.Generic;
using System.Linq;

namespace Sparklings.LevelElements
{
    public class EmptyTile : Tile, IActivable
    {
        public EmptyTile(Level level, int x, int y)
            : base(level, x, y, false)
        {
            lightenedParticles = new ParticleGenerator(level);
            SparklingInducedColor = RayColor.None;
            lightenedParticles.Position = new Vector2(Center.X,PositionLLCorner.Y);
            lightenedParticles.InitialSpeedCalculator =
                () => new Vector2(0.0f, -0.5f * (0.5f+(float)Level.StaticRNG.NextDouble()));
            lightenedParticles.SpeedUpdaterCalculator = (p, t) => p.Speed - p.Speed*(float)t.TotalSeconds;
            lightenedParticles.InitialPositionCalculator = pg => pg.Position + new Vector2((0.5f - (float)Level.StaticRNG.NextDouble()), 0.0f);
        }
        public override void LoadGraphicsRelatedElements()
        {
            base.LoadGraphicsRelatedElements();
            BaseTexture = Level.ContentManager.Load<Texture2D>("air");
            lightenedParticles.ParticleTexture = Level.ContentManager.Load<Texture2D>("colouring_area_particle");
        }
        public override void DrawBase(RenderContext rc)
        {

        }
        public override void DrawAdditive(RenderContext rc)
        {
            lightenedParticles.DrawParticles(rc);
        }
        public override void Update(GameTime gameTime)
        {
            HitColor = HitColor.Combined(SparklingInducedColor);
            lightenedParticles.Update(gameTime);
        }
        public static object ConstructFromData(JsonOverrider converter, JToken toConvert, Level level, int x, int y)
        {
            return new EmptyTile(level, x, y);
        }

        public override bool IsPassableBySparkling(Sparkling c) { return true; }

        public bool Activate()
        {
            Sparkling s = GetCurrentlyContainedSparklings().FirstOrDefault();
            if (s != null)
            {
                SparklingInducedColor = SparklingInducedColor.Combined(s.Color);
                Level.Sparks.Remove(s);
                return true;
            }
            return false;
        }
        public bool IsPlayerActivable { get; set; }

        public List<int> ActivatingInterruptors { get; set; }

        public RayColor SparklingInducedColor
        {
            get
            {
                return sparklingInducedColor;
            }
            protected set
            {
                sparklingInducedColor = value;
                lightenedParticles.CurrentlySpawnedColor = sparklingInducedColor.ToColor();

                if (sparklingInducedColor == RayColor.None)
                {
                    lightenedParticles.TargetParticleCount = 0;
                }
                else
                {
                    lightenedParticles.TargetParticleCount = 20;
                }
            }
        }
        private RayColor sparklingInducedColor;
        protected ParticleGenerator lightenedParticles;
    }
}
